package com.sorhis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sorhis.dto.SysParametersDTO;
import com.sorhis.entity.SysParameter;
import com.sorhis.entity.SysParameters;
import com.sorhis.mapper.SysParametersMapper;
import com.sorhis.service.ISysParametersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 参数子表 服务实现类
 * </p>
 *
 * @author xihao.ding
 * @since 2025-02-25
 */
@Service
public class SysParametersServiceImpl extends ServiceImpl<SysParametersMapper, SysParameters> implements ISysParametersService {

    @Override
    @Transactional
    public Boolean del(String id) {
        LambdaUpdateWrapper<SysParameters> updateWrapper = new LambdaUpdateWrapper<>();
        Assert.notEmpty(id , "ID不能为空");
        updateWrapper.eq(SysParameters::getId , id);
        updateWrapper.set(SysParameters::getDeleted , 1);
        return this.update(updateWrapper);
    }

    @Override
    public IPage findPageList(SysParametersDTO sysParametersDTO) {
        Page<SysParameters> page = new Page(sysParametersDTO.getPageNum(), sysParametersDTO.getPageSize());
        IPage<SysParameters> result = this.page(page ,
                new LambdaQueryWrapper<SysParameters>()
                        .eq(StrUtil.isNotBlank( sysParametersDTO.getPid()) , SysParameters::getPid , sysParametersDTO.getPid())
                        .like(StrUtil.isNotBlank( sysParametersDTO.getParName()) , SysParameters::getParName , sysParametersDTO.getParName())
                        .orderByDesc(SysParameters::getCreateTime));
        return result;
    }

    @Override
    @Transactional
    public Boolean saveOrUpateDomain(SysParametersDTO sysParametersDTO) {
        SysParameters sysParameters = new SysParameters();
        BeanUtil.copyProperties(sysParametersDTO , sysParameters);
        return this.saveOrUpdate(sysParameters);
    }
}
